package com.haitaoit.chengquan.module.center.activity.order;

import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.module.center.adapter.order.TransportstateAdapter;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.MyWebView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("WrongConstant")
public class TransportStateActivity extends BaseActivity {
    //    @ViewInject(R.id.transport_sc)
//    ScrollView transport_sc;
//    @ViewInject(R.id.scrollview_listview)
//    MyListView listView;
    @ViewInject(R.id.transport)
    MyWebView transport;


    private TransportstateAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_transport_state);
        setTitle_V("查看物流");
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);


//        adapter = new TransportstateAdapter(this);
//        listView.setAdapter(adapter);
//        transport_sc.smoothScrollTo(0, 0);

        httpGetorder_model_logistics(getIntent().getStringExtra("order_no"));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (transport.canGoBack() && keyCode == KeyEvent.KEYCODE_BACK) {
            transport.goBack();
            return true;
        }
        finish();
        return super.onKeyDown(keyCode, event);
    }

    private void httpGetorder_model_logistics(String order_no) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("order_no", order_no));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getorder_model_logistics, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                try {
                    JSONObject json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            JSONObject js = new JSONObject(Response);

                            transport.getSettings().setJavaScriptEnabled(true);
                            transport.setScrollBarStyle(0);
                            WebSettings webSettings = transport.getSettings();
                            webSettings.setAllowFileAccess(true);
                            webSettings.setBuiltInZoomControls(true);
                            transport.loadUrl(js.getString("url"));
                            //加载数据
                            transport.setWebChromeClient(new WebChromeClient() {
                                @Override
                                public void onProgressChanged(WebView view, int newProgress) {
                                    if (newProgress == 100) {
                                    }
                                }
                            });
                            //这个是当网页上的连接被点击的时候
                            transport.setWebViewClient(new WebViewClient() {
                                public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
                                    view.loadUrl(url);
                                    return true;
                                }
                            });
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }
}
